<?php

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 	require_once (APPLICATION_PATH . "\\models\\connectionDb.php");
} else {
    require_once (APPLICATION_PATH . "//models//connectionDb.php");
}

class Application_Model_DbTable_Conditionmedicalinfo extends Zend_Db_Table_Abstract
{

    protected $_name = 'Condition_MedicalInfo';
	protected $_primary = 'id';
	
	public function getConditionsForUser($medicaInfo_id)
    {
    	try{
    		
    		$sql ="select Condition_MedicalInfo.*,Conditions.Name,Conditions.Description,Conditions.LatinName";
			$sql .=" from Condition_MedicalInfo";
			$sql .=" left join Conditions on Conditions.id = Condition_MedicalInfo.Condition_id";
			$sql .=" where 1=1";			
			$sql .=" and Condition_MedicalInfo.MedicalInfo_id=".$medicaInfo_id;    		 
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
	public function getConditionsNonActiveForUser($medicaInfo_id)
    {
    	try{
    		
    		$sql ="select Conditions.id,Conditions.Name";
			$sql .=" from Conditions";
			$sql .=" where Conditions.id NOT IN (select Condition_MedicalInfo.Condition_id";
			$sql .=" from Conditions";			
			$sql .=" inner join Condition_MedicalInfo on Condition_MedicalInfo.Condition_id=Conditions.id";  
			$sql .=" where 1=1";
			$sql .=" and Condition_MedicalInfo.MedicalInfo_id=".$medicaInfo_id.")";	 
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }    
    }
    
	public function AddCondtitionForUser($medicalInfo,$condition, $dateCondtion)
    {
    	$data = array(
    		'MedicalInfo_id' => $medicalInfo,
    		'Condition_id' => $condition,
    		'DateCondtion' => $dateCondtion,    		
    	);
    	$this->insert($data);
    }
	public function deleteCondtionForUser($id)
    {	
    	try {
    		$this->delete('id = ' . (int)$id);
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    	
    }
}

